#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
Credit to 长安1206@欢乐窝
'''

import git
import os
import datetime, pytz
from apscheduler.schedulers.blocking import BlockingScheduler

# Get current time
def get_current_time():
    tz_shanghai = pytz.timezone('Asia/Shanghai')
    now = datetime.datetime.now(tz = tz_shanghai)
    current_time = now.strftime("%Y-%m-%d-%Hh%M")
    return current_time

# 
def upload_git():
    current_dir = os.path.dirname(__file__)
    repo = git.Repo(current_dir)
    remote = repo.remote()
    
    # remote.pull()
    repo.git.add('-A')
    repo.git.commit('-m', 'Upload '+ get_current_time())
    remote.push()

def auto_upload():
    print("Start uploading...")
    print(datetime.datetime.now())
    try:
        upload_git()
    except:
        print("Upload error...")
    print("Upload finished")
        
def dojob():
    #Create scheduler: BlockingScheduler
    scheduler = BlockingScheduler()
    #Add job with interval = 3 hours
    scheduler.add_job(auto_upload, 'interval', hours = 3, id=' Auto_update_folder',
                      next_run_time=datetime.datetime.now() + datetime.timedelta(seconds=10))
    scheduler.start()

if __name__ == '__main__':
    dojob()

